package design.proxy.three;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * CGlib代理类
 *
 * @Author: zhenzhong.wang
 * @Time: 2018/4/10 11:19
 */
public class CGlibProxyFactory implements MethodInterceptor {

  // 代理对象
  private Object target;

  public CGlibProxyFactory(Object target) {
    this.target = target;
  }

  @Override
  public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    System.out.println("before JDKProxyFactory");
    Object invoke = method.invoke(target, args);
    System.out.println("after JDKProxyFactory");
    return invoke;
  }

  /**
   * 获得代理实例
   *
   * @return
   */
  public Object getProxyInstance() {
    Enhancer en = new Enhancer();
    en.setSuperclass(target.getClass());
    en.setCallback(this);
    return en.create();
  }
}
